import router from './index';
import { useUserStore } from '@/store/user.js';

const whiteList = ['/login'];
router.beforeEach(async (to, from, next) => {
  const user = useUserStore();
  console.log('to', to, user.token);
  //存在token
  if (user.token) {
    if (to.path === '/login') {
      next('/');
    } else {
      //是否有用户信息
      if (!user.userInfo) {
        await user.set_userInfo();
        return next(to.path);
      }
      next();
    }
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next('/login');
    }
  }
});
